package com.moss.cloud.base.biz.controller;

import com.moss.cloud.base.api.model.from.BaseFrom;
import com.moss.cloud.base.api.model.from.DeptFrom;
import com.moss.cloud.base.api.model.param.DeptParam;
import com.moss.cloud.base.biz.service.ISysDeptService;
import com.moss.cloud.common.core.constant.ServiceList;
import com.moss.cloud.common.core.model.Result;
import com.moss.cloud.common.security.annotation.SysLog;
import com.moss.cloud.common.security.annotation.XApiToken;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * 部门管理前端控制器
 *
 * @author 瑾年
 * @date 2023年3月23日
 */
@RestController
@RequestMapping(ServiceList.BASE + "/sysDept")
public class SysDeptController {
    private final ISysDeptService sysDeptService;

    public SysDeptController(ISysDeptService sysDeptService) {
        this.sysDeptService = sysDeptService;
    }

    /**
     * 根据当前部门ID查找当前部门及其子部门
     *
     * @param deptId
     * @return
     */
    @GetMapping("/sysDepts")
    @SysLog(serverName = ServiceList.MOSS_CLOUD_BASE, operation = "根据当前部门ID查找当前部门及其子部门")
    public Result<?> sysDepts(Long deptId) {
        return Result.success(sysDeptService.sysDepts(deptId));
    }

    /**
     * 租户部门树查询
     *
     * @param deptParam
     * @return
     */
    @PostMapping("/deptTree")
    @SysLog(serverName = ServiceList.MOSS_CLOUD_BASE, operation = "租户部门树查询")
    public Result<?> deptTree(@RequestBody @Validated DeptParam deptParam) {
        return Result.success(sysDeptService.deptTree(deptParam));
    }

    /**
     * 根据部门查找部门信息
     *
     * @param baseFrom
     * @return
     */
    @PostMapping("/deptInfo")
    @SysLog(serverName = ServiceList.MOSS_CLOUD_BASE, operation = "根据部门查找部门信息")
    public Result<?> deptInfo(@RequestBody @Validated BaseFrom baseFrom) {
        return Result.success(sysDeptService.findById(baseFrom.getId()));
    }

    /**
     * 租户部门树全量数据查询
     *
     * @param deptParam
     * @return
     */
    @PostMapping("/allDeptTree")
    @SysLog(serverName = ServiceList.MOSS_CLOUD_BASE, operation = "租户部门树全量数据查询")
    public Result<?> allDeptTree(@RequestBody @Validated DeptParam deptParam) {
        return Result.success(sysDeptService.allDeptTree(deptParam));
    }

    /**
     * 新增部门
     *
     * @param deptFrom
     * @return
     */
    @PostMapping("/saveDept")
    @XApiToken
    @SysLog(serverName = ServiceList.MOSS_CLOUD_BASE, operation = "新增部门")
    public Result<?> saveDept(@RequestBody @Validated DeptFrom deptFrom){
        return Result.success(sysDeptService.saveDept(deptFrom));
    }

    /**
     * 更新部门
     *
     * @param deptFrom
     * @return
     */
    @PostMapping("/updateDept")
    @XApiToken
    @SysLog(serverName = ServiceList.MOSS_CLOUD_BASE, operation = "更新部门")
    public Result<?> updateDept(@RequestBody @Validated DeptFrom deptFrom){
        return Result.success(sysDeptService.updateDept(deptFrom));
    }

    /**
     * 删除部门
     *
     * @param baseFrom
     * @return
     */
    @PostMapping("/removeDept")
    @SysLog(serverName = ServiceList.MOSS_CLOUD_BASE, operation = "删除部门")
    public Result<?> removeDept(@RequestBody @Validated BaseFrom baseFrom){
        return Result.success(sysDeptService.removeDept(baseFrom));
    }
}
